﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2012-3-21
/// Purpose: 		扩展Mvc Controller的方法
/// ****************************************************************************
/// Modify By		Date			Remark
/// ****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace RB.Web.Mvc
{
    /// <summary>
    /// Controller扩展类
    /// </summary>
    public static class ControllerExtensions
    {
        /// <summary>
        /// 使用操作名称，通过Json数据要求客户端重定向到指定的操作
        /// </summary>
        /// <param name="controller">当前控制器类实例</param>
        /// <param name="actionName">操作的名称</param>
        /// <returns>Json格式的重定向结果对象</returns>
        public static ActionResult AjaxRedirectToAction(this Controller controller, string actionName)
        {
            return AjaxRedirectToAction(controller, actionName, null, null);
        }

        /// <summary>
        /// 使用操作名称和路由值，通过Json数据要求客户端重定向到指定的操作
        /// </summary>
        /// <param name="controller">当前控制器类实例</param>
        /// <param name="actionName">操作的名称</param>
        /// <param name="routeValues">路由的参数</param>
        /// <returns>Json格式的重定向结果对象</returns>
        public static ActionResult AjaxRedirectToAction(this Controller controller, string actionName, object routeValues)
        {
            return AjaxRedirectToAction(controller, actionName, null, routeValues);
        }

        /// <summary>
        /// 使用操作名称、控制器名称和路由值，通过Json数据要求客户端重定向到指定的操作
        /// </summary>
        /// <param name="controller">当前控制器类实例</param>
        /// <param name="actionName">操作的名称</param>
        /// <param name="controllerName">控制器的名称</param>
        /// <param name="routeValues">路由的参数</param>
        /// <returns>Json格式的重定向结果对象</returns>
        public static ActionResult AjaxRedirectToAction(this Controller controller, string actionName, string controllerName, object routeValues)
        {
            string actionUrl = controller.Url.Action(actionName, controllerName, routeValues);
            return AjaxRedirect(controller, actionUrl, false);
        }

        public static ActionResult AjaxView(this Controller controller)
        {
            return AjaxView(controller, null);
        }

        public static ActionResult AjaxView(this Controller controller, object model)
        {
            ViewResultBase result = null;
            if (controller.Request.IsAjaxRequest())
            {
                result = new PartialViewResult();
            }
            else
            {
                result = new ViewResult();
            }
            if (model != null)
            {
                controller.ViewData.Model = model;
            }
            result.ViewData = controller.ViewData;

            return result;
        }

        public static ActionResult AjaxRedirect(this Controller controller, string url, bool external)
        {
            if (controller.Request.IsAjaxRequest())
            {
                JsonResult result = new JsonResult();
                result.Data = new
                {
                    redirect = true,
                    external = external,
                    url = url
                };
                return result;
            }
            else
            {
                return new RedirectResult(url);
            }
        }
    }
}
